﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Healthy
{
    public partial class frmPrenatal : Form
    {
        public frmPrenatal()
        {
            InitializeComponent();
        }

        private void frmPrenatal_Load(object sender, EventArgs e)
        {

            string oMySqlConn = (@"Server=narma-pc\sqlexpress;Database=mydata;Integrated Security=SSPI");
            SqlConnection Conn = new SqlConnection(oMySqlConn);

            showType();
            //autoid();
        }

        private void showType()
        {
            string oMySqlConn = (@"Server=narma-pc\sqlexpress;Database=mydata;Integrated Security=SSPI");
            SqlConnection Conn = new SqlConnection(oMySqlConn);
            Conn.Open();

            string sqlType1;
            sqlType1 = "select * from doctor ";

            SqlDataAdapter da;
            DataSet ds = new DataSet();
            da = new SqlDataAdapter(sqlType1, Conn);
            da.Fill(ds, "doctor");

            cmbdc.DataSource = ds.Tables["doctor"];
            cmbdc.ValueMember = "DCID";
            cmbdc.DisplayMember = "DCNAM";

        }
        private void showdata1()
        {
            string oMySqlConn = (@"Server=narma-pc\sqlexpress;Database=mydata;Integrated Security=SSPI");
            SqlConnection Conn = new SqlConnection(oMySqlConn);
            Conn.Open();

            string sql1 = "select * from person where PNNAM = '" + name.Text + "'";
            SqlCommand cmd1 = new SqlCommand(sql1, Conn);
            SqlDataReader reader = cmd1.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    comboBox1.Text = reader["HN"].ToString();
                    comboBox2.Text = reader["IDCARD"].ToString();
                    comboBox3.Text = reader["PNNAM"].ToString();
                    bdate.Text = reader["BIRTH"].ToString();
                    add.Text = reader["ADDRESS"].ToString();


                }

                reader.Close();

            }


        }
        private void button13_Click(object sender, EventArgs e)
        {
            if (name.Text == "")
            {
               MessageBox.Show("กรุณากรอกรายชื่อต้องการค้นหา !!!", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error);
               return;
            }
            else
            {
                
                showdata1();
            }
        }
      

        private void insert_Click(object sender, EventArgs e)
        {
            string oMySqlConn = (@"Server=narma-pc\sqlexpress;Database=mydata;Integrated Security=SSPI");
            SqlConnection Conn = new SqlConnection(oMySqlConn);
            Conn.Open();

            string strdate = Convert.ToString(fdate.Value.Year) + "/" + Convert.ToString(fdate.Value.Month) + "/" + Convert.ToString(fdate.Value.Day);//วันที่
            string strdate1 = Convert.ToString(dateTimePicker1.Value.Year) + "/" + Convert.ToString(dateTimePicker1.Value.Month) + "/" + Convert.ToString(dateTimePicker1.Value.Day);//วันที่
            if (name.Text != "" && grav.Text != "")
            {

                string strOut = "";
                strOut += "เลขที่การฝากครรภ์ :" + prcid.Text + "\n";
                strOut += "ชื่อ-สกุล :" + name.Text + "\n";
                strOut += "ครรภ์ที่ :" + grav.Text + "\n";
                strOut += "แพทย์ผู้รับผิดชอบ :" + cmbdc.SelectedValue + "\n";
                strOut += "วันที่มาฝาก :" + fdate.Value.ToLongDateString() + "\n";
                strOut += "วันที่กำหนดคลอด :" + dateTimePicker1.Value.ToLongDateString() + "\n";

                MessageBox.Show(strOut, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

                string query = "insert into prenatal_care values('" + prcid.Text + "' ,'" + comboBox1.Text + "' , '" + grav.Text + "' ,'" + strdate + "','" + cmbdc.SelectedValue + "','" + strdate1+ "' )";
                SqlCommand cmd = new SqlCommand(query, Conn);

                //Use ExecuteNonQuery to insert data.   
                cmd.ExecuteNonQuery();


                string sql1 = "SELECT PRCID,HN,PNFNAM,GRAVIDA,DATAPP,FDATE,DCFNM,DISC FROM  prenatal_care,doctor,person,prenatal WHERE prenatal_care.DCID=doctor.DCID and prenatal_care.HN=person.HN and prenatal_care.PRCID=prenatal.PRCID and ((prenatal_care.PRCID)='" + prcid.Text + "') ";
                SqlCommand cmd1 = new SqlCommand(sql1, Conn);
                SqlDataAdapter odbcDA = new SqlDataAdapter(cmd1);
                DataSet ds = new DataSet();

            }
            else
            {
                MessageBox.Show("ข้อมูลไม่ครบ", "ผิดพลาด");
            }
            MessageBox.Show("ข้อมูลถูกบันทึกเรียบร้อยแล้ว");
        }

        private void cleardata()
        {
            prcid.Text = "";
            comboBox1.Text = "";
            comboBox2.Text = "";
            comboBox3.Text = "";
            bdate.Text = "";
            add.Text = "";
            grav.Text = "";
            cmbdc.Text = "";
            fdate.Text = "";
        }


        private void clear_Click(object sender, EventArgs e)
        {
            cleardata();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmd_prenatal f4 = new frmd_prenatal();
            f4.ShowDialog();

        }

       

      

        private void button2_Click(object sender, EventArgs e)
        {

            frmperson f5 = new frmperson();
            f5.ShowDialog();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
